﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.Slaughter_屠宰_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Slaughter_屠宰_
{
    public class UpdateCompanyHandler : IRequestHandler<UpdateCompanyCommand, ApiResult<int>>
    {
        private readonly IBase<CompanyModel> companyRepo;
        private readonly IMapper mapper;

        public UpdateCompanyHandler(IBase<CompanyModel>companyRepo,IMapper mapper)
        {
            this.companyRepo = companyRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdateCompanyCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var company = companyRepo.GetValue(request.CompanyId).Result;
             company = mapper.Map(request,company);
            var r=companyRepo.Update(company).Result;
            if(r>0)
            {
                result.Code = ApiEnum.OK;
                result.Msg = "修改屠宰企业信息成功";
                result.Data = r;
                return Task.FromResult(result);
            }    
            else
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "修改屠宰企业信息失败";
                result.Data = r;
                return Task.FromResult(result);
            }
        }
    }
}
